﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;
using VietPay.KooBoo.Data.Infrastructure;
using VietPay.KooBoo.Business.Models;
using VietPay.KooBoo.Business.Interfaces;
using VietPay.KooBoo.Data.Interfaces;
using VietPay.KooBoo.Data;
using VietPay.KooBoo.Data.EntityModels;
using System.Transactions;
using VietPay.KooBoo.Common;
using Newtonsoft.Json;

namespace VietPay.KooBoo.Business
{
    public class BatchDetailService : ServiceBase<IBatchDetailModel>, IBatchDetailService
    {
        public BatchDetailService()
            : this(new BatchDetailRepository())
        {
        }

        private IBatchDetailRepository _repository;

        public BatchDetailService(IBatchDetailRepository batchRepository)
        {
            _repository = batchRepository ?? new BatchDetailRepository();          
        }

        protected override Type LogPrefix
        {
            get { return this.GetType(); }
        }

        public BatchDetailListViewModel GetBatchDetailList(int batchID, int PageSize, int CurrentPage)
        {
            try
            {
                if ((CurrentPage == 0) || (PageSize == 0))
                    return null;

                var vms = new VMS2_DistributorEntities();
                var result = (from bd in vms.BatchDetails
                              join p in vms.Products on bd.ProductID equals p.ProductID// into g
                              where bd.BatchID == batchID
                              select new BatchDetailViewModel
                              {
                                  BatchID = bd.BatchID,
                                  ProductID = bd.ProductID,
                                  ProductName = p.ProductName,
                                  Quantity = bd.Quantity,
                                  QuantityBalance = bd.QuantityBalance,
                                  Total = bd.Quantity * bd.Price
                                  //Quantity = g.Sum(p => p.Quantity)
                              });

                List<BatchDetailViewModel> lst = result.ToList<BatchDetailViewModel>();

                BatchDetailListViewModel model = new BatchDetailListViewModel();
                model.TotalItemCount = lst.Count();
                if (model.TotalItemCount == 0)
                    return null;
                model.TotalPageCount = (int)Math.Ceiling((double)model.TotalItemCount / (double)PageSize);
                if (model.TotalPageCount != 1)
                    lst = lst.OrderBy(x => x.BatchID).Skip((CurrentPage - 1) * PageSize).Take(PageSize).ToList();

                model.BatchDetailList = lst;

                return model;
            }
            catch (System.Exception e)
            {
                this.LogError("Error getting the BatchDetail list", e);
                return null;
            }
        }

        public BatchDetailViewModel GetSingle(Expression<Func<BatchDetailViewModel, bool>> whereCondition)
        {
            throw new NotImplementedException();
        }

        public void Add(BatchDetailViewModel vModel)
        {
            BatchDetail entity = new BatchDetail();
            _repository.Add(entity);
        }

        public void Delete(BatchDetailViewModel vModel)
        {
            BatchDetail news = _repository.GetAll(p => p.BatchID == vModel.BatchID).FirstOrDefault();
            _repository.Delete(news);
        }

        public void Update(BatchDetailViewModel vModel)
        {
            BatchDetail entity = _repository.GetAll(p => p.BatchID == vModel.BatchID).FirstOrDefault();            
            _repository.Attach(entity);
        }

        public IList<BatchDetailViewModel> GetAll(Expression<Func<BatchDetailViewModel, bool>> whereCondition)
        {
            throw new NotImplementedException();
        }

        public IList<BatchDetailViewModel> GetAll()
        {
            throw new NotImplementedException();
        }

        public IQueryable<BatchDetailViewModel> Query(Expression<Func<BatchDetailViewModel, bool>> whereCondition)
        {
            List<BatchDetailViewModel> newsQuery = new List<BatchDetailViewModel>();
            IQueryable<BatchDetail> query = _repository.GetQueryable();
            //AutoMapper.Mapper.CreateMap<BatchDetail, BatchDetailViewModel>()
            //    .ForMember(dest => dest.BatchID, opt => opt.MapFrom(src => src.BatchID))
            //        .ForMember(dest => dest.BatchCode, opt => opt.MapFrom(src => src.BathCode))
            //        .ForMember(dest => dest.CreatedDate, opt => opt.MapFrom(src => src.CreatedDate))
            //        .ForMember(dest => dest.DistributorID, opt => opt.MapFrom(src => src.DistributorID))
            //        .ForMember(dest => dest.ParentDistributorID, opt => opt.MapFrom(src => src.ParentDistributorID))
            //        .ForMember(dest => dest.Description, opt => opt.MapFrom(src => src.Description));
            //newsQuery = AutoMapper.Mapper.Map(query, newsQuery);
            return newsQuery.AsQueryable().Where(whereCondition);
        }

        public long Count(Expression<Func<BatchDetailViewModel, bool>> whereCondition)
        {
            throw new NotImplementedException();
        }

        public long Count()
        {
            throw new NotImplementedException();
        }
    }
}
